#include "linetest.h"

#include "microscheme.h"
#include "realelement.h"
#include "inelement.h"
#include "instantiator.h"

#include <QDebug>

LineTest::LineTest() : TestCase("Тест логики NAND2",0,6)
{
}

QVector<SignalContainer*> LineTest::testIt()
{    
    Instantiator& factory = Instantiator::getInstance();

    MicroScheme* scheme = new MicroScheme();
    scheme->ticPerTact = 1;
    scheme->tactCount = timeLength;

    RealElement* nand = (RealElement*)factory.buildElement("nand2");
    scheme->appendObject(nand);

    QImage* qimg = nand->getImage()->getBufferImage();
    qimg->save("ololo.png");

    InElement* input1 = (InElement*)factory.buildElement("input");
    scheme->appendObject(input1);

    InElement* input2 = (InElement*)factory.buildElement("input");
    scheme->appendObject(input2);

    //Нудно добавляем сигналы для тестов в источники

    SignalContainer& a_signal = input1->getSignalCollection();
    a_signal.addSignals("0 1 0 1 0 1");

    SignalContainer& bSignal = input2->getSignalCollection();
    bSignal.addSignals("0 0 1 1 1 1");

    input1->pins.at(0)->connectPin(nand->pins.at(0));
    input2->pins.at(0)->connectPin(nand->pins.at(1));

    SignalContainer* watches[3];

    watches[0] = new SignalContainer(input1->pins.at(0));
    watches[1] = new SignalContainer(input2->pins.at(0));
    watches[2] = new SignalContainer(nand->pins.at(2));

    SignalContainer* res[3];
    res[0] = new SignalContainer(input1->pins.at(0));
    res[0]->label = "Input1";
    res[1] = new SignalContainer(input2->pins.at(0));
    res[1]->label = "Input2";
    res[2] = new SignalContainer(nand->pins.at(2));
    res[2]->label = "NAND_2";

    for (int i = 0; i < 3; i++)
        scheme->watches.append(watches[i]);

    scheme->run();

    QVector<SignalContainer*> results;

    //Косяк, нужно скопировать данные
    for (int j = 0; j < 3; j++)
    {
        for (int i = 0; i < watches[j]->getItems().count(); i++)
        {
            res[j]->addInterval(watches[j]->getItems().at(i));
        }
        results.append(res[j]);
    }

    delete scheme;

    return results;
}
